package com.tomecode.tools.maven.settings.model;

/**
 * Download policy
 * 
 * @author Tomas Frastia
 * @see http://www.tomecode.com or
 *      http://code.google.com/p/maven-settings-editor/
 * 
 */
public final class Snapshot {
	/**
	 * Whether to use this repository for downloading this type of artifact.
	 */
	private boolean enabled = true;
	/**
	 * The frequency for downloading updates - can be "always", "daily"
	 * (default), "interval:XXX" (in minutes) or "never" (only if it doesn't
	 * exist locally).
	 */
	private UpdatePolicy updatePolicy;
	/**
	 * What to do when verification of an artifact checksum fails - warn, fail,
	 * etc. Valid values are "fail" or "warn".
	 */
	private ChecksumPolicy checksumPolicy;

	private int updatePolicyInterval = 0;

	public Snapshot() {
	}

	/**
	 * Copy exists {@link Snapshot} to new {@link Snapshot}
	 * 
	 * @param snapshots
	 */
	public Snapshot(Snapshot snapshots) {
		updatePolicy = snapshots.getUpdatePolicy();
		updatePolicyInterval = snapshots.getUpdatePolicyInterval();
		enabled = snapshots.isEnabled();
		checksumPolicy = snapshots.getChecksumPolicy();
	}

	public final UpdatePolicy getUpdatePolicy() {
		return updatePolicy;
	}

	public final void setUpdatePolicy(String updatePolicy) {
		this.updatePolicy = UpdatePolicy.parse(updatePolicy);
		if (this.updatePolicy == UpdatePolicy.INTERVAL) {
			updatePolicyInterval = UpdatePolicy.parseInterval(updatePolicy);
		}
	}

	public final ChecksumPolicy getChecksumPolicy() {
		return checksumPolicy;
	}

	public final void setChecksumPolicy(String checksumPolicy) {
		this.checksumPolicy = ChecksumPolicy.parse(checksumPolicy);
	}



	public final void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}

	public final boolean isEnabled() {
		return enabled;
	}

	public final int getUpdatePolicyInterval() {
		return updatePolicyInterval;
	}

	public final String makeUpdatePolicy() {
		if (updatePolicy == UpdatePolicy.INTERVAL) {
			return UpdatePolicy.getInterval(updatePolicyInterval);
		} else if (updatePolicy == null) {
			return "";
		}
		return updatePolicy.toString();
	}
}
